const Controller = require("egg").Controller;
const errorLog = require("../utils/errorLog");

class noticeController extends Controller {
  index() {
    try {
      this.ctx.body = {
        code: 200,
        msg: "测试成功",
      };
    } catch (err) {
      errorLog("NoticeController----->index", err);
      return false;
    }
  }

  async createNotice() {
    try {
      const noticeObj = this.ctx.request.body;
      if (
        !noticeObj ||
        !noticeObj.title ||
        noticeObj.title === "" ||
        !noticeObj.content ||
        noticeObj.content === ""
      ) {
        this.ctx.body = { code: 500, msg: "标题或内容不能为空" };
        return false;
      }

      const notice = await this.ctx.service.notice.createNotice(noticeObj);
      if (notice) {
        this.ctx.body = { code: 200, msg: "创建成功" };
      } else {
        this.ctx.body = { code: 500, msg: "创建失败" };
      }
    } catch (err) {
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
      errorLog("NoticeController----->createNotice", err);
    }
  }

  async getNoticeList() {
    try {
      const { currentPage, pageSize } = this.ctx.request.body;
      const result = await this.ctx.service.notice.getNoticeList(
        currentPage,
        pageSize
      );
      if (result) {
        this.ctx.body = { code: 200, msg: "获取成功", data: result };
      } else {
        this.ctx.body = { code: 500, msg: "获取失败" };
      }
    } catch (err) {
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
      errorLog("NoticeController----->getNoticeList", err);
    }
  }

  async getNoticeListByNoticeTitle() {
    try {
      const { noticeTitle, currentPage, pageSize } = this.ctx.request.body;
      const result = await this.ctx.service.notice.getNoticeListByNoticeTitle(
        noticeTitle,
        currentPage,
        pageSize
      );
      if (result) {
        this.ctx.body = { code: 200, msg: "获取成功", data: result };
      } else {
        this.ctx.body = { code: 500, msg: "获取失败" };
      }
    } catch (err) {
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
      errorLog("NoticeController----->getNoticeListByNoticeTitle", err);
    }
  }

  async updateNotice() {
    try {
      const noticeObj = this.ctx.request.body;
      const result = await this.ctx.service.notice.updateNotice(noticeObj);
      if (result) {
        this.ctx.body = { code: 200, msg: "修改成功" };
      } else {
        this.ctx.body = { code: 500, msg: "修改失败" };
      }
    } catch (err) {
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
      errorLog("noticeController----->updateNotice");
    }
  }

  async deleteNotice() {
    try {
      const { id } = this.ctx.request.query;
      if (id) {
        const result = await this.ctx.service.notice.deleteNotice(id);
        if (result) {
          this.ctx.body = { code: 200, msg: "删除成功" };
        } else {
          this.ctx.body = { code: 500, msg: "删除失败" };
        }
      } else {
        this.ctx.body = { code: 500, msg: "缺少参数" };
      }
    } catch (err) {
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
      errorLog("noticeController----->deleteNotice");
    }
  }
}

module.exports = noticeController;
